package com.lg.controller;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lg.model.AngelCandidate;
import com.lg.model.AngelComment;
import com.lg.model.AngelUser;
import com.lg.serivce.AngelCandidateService;
import com.lg.serivce.AngelCommentService;
import com.lg.serivce.AngelPicturesService;
import com.lg.serivce.AngelUserService;
import com.lg.serivce.StPromoterService;
import com.lg.util.PageUtilBean;
@Controller
public class AngelController extends BaseController {

	@Resource(name="angelCandidateService")
	private AngelCandidateService angelCandidateService;
	@Resource(name="angelCommentService")
	private AngelCommentService  angelCommentService;
	@Resource(name="angelPicturesService")
	private AngelPicturesService angelPicturesService;
	@Resource(name = "angelUserService")
	private AngelUserService angelUserService;
	@Resource(name = "stPromoterService")
	private StPromoterService stPromoterService;
	/**
	 * 查询用户空间
	 */
	@RequestMapping("/personspace.do")
	public String findPersonSpace(HttpSession session,String userid,String page,String type,ModelMap map){
		
		userid = "PT000008";
		AngelCandidate angel = angelCandidateService.findAngelCandidate(userid);//用户信息
		
		if(type==null){
			PageUtilBean comments = angelCommentService.findAngelComments(null, userid);//评论集合
			PageUtilBean photos =  angelPicturesService.findPromoterPhotos(userid, null);//图片
			map.addAttribute("comments", comments);
			map.addAttribute("photos", photos);
		}else if(type.equals("comment")){
			PageUtilBean comments = angelCommentService.findAngelComments(null, userid);//评论集合
			PageUtilBean photos =  angelPicturesService.findPromoterPhotos(userid, page);//图片
			map.addAttribute("comments", comments);
			map.addAttribute("photos", photos);
		}else if(type.equals("photo")){
			PageUtilBean comments = angelCommentService.findAngelComments(page, userid);//评论集合
			PageUtilBean photos =  angelPicturesService.findPromoterPhotos(userid, null);//图片
			map.addAttribute("comments", comments);
			map.addAttribute("photos", photos);
		}
		List voiteTypeList = angelCandidateService.findVoteByPromoterId(userid);
		map.addAttribute("voiteTypeList", voiteTypeList);
		map.addAttribute("angel", angel);
		return "personspace1";
	}
	@RequestMapping("/findAngels.do")
	public String findAngels(String type,ModelMap map){
		//1或空为慧智天使   2为印象天使  3为微笑天使
		type = type==null?"1":type;
		List angelList = angelCandidateService.findAngels(type);
		map.addAttribute("angelList", angelList);
		return "angels";
	}
	@RequestMapping("/addComment.do")
	public String addComment(AngelComment c,HttpServletRequest request,String regid, String pwd) throws SQLException{
		
		String niming = request.getParameter("niming");
		if(niming!=null){
			c.setUserId(null);
		}else{
			AngelUser user = angelUserService.findUserInLogin(regid, pwd);
		}
		
		UUID uuid = UUID.randomUUID();
		c.setCommentId(uuid.toString());
		c.setCommentDt(new Date());
		
		String ip = request.getLocalAddr();
		c.setCommentIp(ip);
		
		angelCommentService.add(c);
		return "redirect:personspace.do";
	}
	/**
	 * 异步判断用户名和密码是否正确
	 */
	@RequestMapping("/judgeUser.do")
	public void judgeUser(String regId,String userPw,String userType, HttpServletResponse response){
		AngelUser user = angelUserService.findUserInLogin(regId, userPw);
		String result = user==null?"0":"1";
		
		ajaxOutput(response, result);
	}
	/**
	 * 修改选手自我描述资料
	 */
	@RequestMapping("/updateUser.do")
	public void updateAngel(HttpSession session,String description){
		
		AngelCandidate user = (AngelCandidate)session.getAttribute("user");
		user.setDescription(description);
		angelCandidateService.updateAngel(user);
	}
	@RequestMapping("/delPhoto.do")
	public String delPhoto(HttpSession session,String pictureId,String filepath){
		angelPicturesService.delPhoto(pictureId, filepath);
		
		return "redirect:personspace.do";
		
	}
}
